package com.wjk.cms.util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class RealnameUtil {

    private static final String DEC_KEY = "w7612qnn21apzs53W36HGMxh";
    private static final String APP_KEY = "I6hZ03RAs73SY5AV9M16X91E2lvqkFcR";
    private static final String DEALER_ID = "08871914";
    private static final String BROKER_ID = "yiyun73";
    private static final String YUN_URL = "https://api-jiesuan.yunzhanghu.com/api/payment/v1/order-realtime";
    private static final String YUN_BANKCARD_3F = "https://api-jiesuan.yunzhanghu.com/authentication/verify-bankcard-three-factor";
    private static final String YUN_REALNAME = "https://api-jiesuan.yunzhanghu.com/authentication/verify-id";

    public static boolean verify(String name, String idCardNo, String bankCardNo) {
        JSONObject data = new JSONObject();
        data.put("real_name", name);
        data.put("id_card", idCardNo);
        data.put("card_no", bankCardNo);
        if (!"0000".equals(JSONObject.parseObject(RealnameUtil.requestSignPost(YUN_BANKCARD_3F, data.toJSONString())).getString("code"))) {
            return false;
        }
        return true;
    }

    public static boolean verify(String name, String idCardNo) {
        JSONObject data = new JSONObject();
        data.put("real_name", name);
        data.put("id_card", idCardNo);
        if (!"0000".equals(JSONObject.parseObject(RealnameUtil.requestSignPost(YUN_REALNAME, data.toJSONString())).getString("code"))) {
            return false;
        }
        return true;
    }

    private static String requestSignPost(String url, String content) {
        log.info(content);

        String dealer_id = DEALER_ID;
        String broker_id = BROKER_ID;
        String deckey = DEC_KEY;
        String appkey = APP_KEY;

        // 首先对参数进行加密
        String secBodyStr = "";
        try {
            byte[] bytes = PaysignUtil.tripleDesEncrypt(content.getBytes("utf-8"), deckey.getBytes("utf-8"));
            byte[] enc64 = Base64.encodeBase64(bytes);
            String data = new String(enc64);
            //生成签名
            String mess = CommonUtil.randomNumber(6) + ""; // 6位随机数
            String timestamp = System.currentTimeMillis() + "";
            String signstr = "data=" + data + "&mess=" + mess + "&timestamp=" + timestamp + "&key=" + appkey;
            String s = HmacSha256Pay.sha256_HMAC(signstr, appkey);
            CloseableHttpClient webClient = null;
            HttpUtil httpUtils = new HttpUtil();
            webClient = httpUtils.createSSLClientDefault(true);
            long systime = System.currentTimeMillis();
            Map<String, String> params = new HashMap<>();
            params.put("request-id", timestamp + "01");
            params.put("dealer-id", dealer_id);
            Map<String, String> headerMap = initHeader(params);
            List<NameValuePair> query_params = new ArrayList<NameValuePair>();
            query_params.add(new BasicNameValuePair("data", data));
            query_params.add(new BasicNameValuePair("mess", mess));
            query_params.add(new BasicNameValuePair("timestamp", timestamp));
            query_params.add(new BasicNameValuePair("sign", s));
            query_params.add(new BasicNameValuePair("sign_type", "sha256"));
            Map<String, Object> secBody = null;
            secBody = httpUtils.sendHttpPost(webClient, url, headerMap, query_params, true, systime);
            HttpEntity secEntity = (HttpEntity) secBody.get("httpEntity");
            secBodyStr = EntityUtils.toString(secEntity);

            log.info(secBodyStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return secBodyStr;
    }

    private static Map<String, String> initHeader(Map<String, String> headerMap) {
        headerMap.put("user-agent", "PostmanRuntime/7.13.0");
        headerMap.put("Accept", "*/*");
        headerMap.put("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
        headerMap.put("Content-Type", "application/x-www-form-urlencoded");
        headerMap.put("Accept-Encoding", "gzip, deflate");
        headerMap.put("Connection", "keep-alive");
        headerMap.put("Host", "api-jiesuan.yunzhanghu.com");
        headerMap.put("cache-control", "no-cache");
        return headerMap;
    }

}
